package com.dstz.bpm.engine.listener;

import cn.hutool.core.lang.Assert;
import com.dstz.bpm.act.listener.ActEventListener;
import com.dstz.bpm.api.constant.ActionType;
import com.dstz.bpm.api.engine.context.BpmContext;
import com.dstz.bpm.api.service.BpmProcessDefService;
import com.dstz.bpm.core.manager.BpmDefinitionManager;
import com.dstz.bpm.core.manager.BpmInstanceManager;
import com.dstz.bpm.core.manager.BpmTaskStackManager;
import com.dstz.bpm.core.model.BpmInstance;
import com.dstz.bpm.engine.action.cmd.DefualtTaskActionCmd;
import com.dstz.bpm.engine.data.handle.IBpmBusDataHandle;
import com.dstz.bus.api.model.IBusinessPermission;
import javax.annotation.Resource;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.delegate.event.impl.ActivitiEntityEventImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AutomaticTaskListener implements ActEventListener {
   @Resource
   private BpmInstanceManager bb;
   @Resource
   private BpmDefinitionManager c;
   @Resource
   private BpmTaskStackManager k;
   @Autowired
   IBpmBusDataHandle aV;
   @Autowired
   BpmProcessDefService a;

   public void notify(ActivitiEvent event) {
      ActivitiEntityEventImpl activitEvent = (ActivitiEntityEventImpl)event;
      BpmInstance instance = this.bb.getByActInstId(activitEvent.getProcessInstanceId());
      Assert.notNull(instance, "TIMER_FIRED Event 流程实例丢失! Activiti ProcessInstanceId %s", new Object[]{activitEvent.getProcessInstanceId()});
      DefualtTaskActionCmd taskComplateCmd = new DefualtTaskActionCmd();
      taskComplateCmd.setActionName(ActionType.TASKCANCELLED.getKey());
      taskComplateCmd.setBpmInstance(instance);
      taskComplateCmd.setBpmDefinition(this.a.getDefinitionById(instance.getDefId()));
      taskComplateCmd.setBizDataMap(this.aV.getInstanceBusData(instance.getId(), (IBusinessPermission)null));
      taskComplateCmd.setOpinion("任务进入定时事件");
      BpmContext.cleanTread();
      BpmContext.setActionModel(taskComplateCmd);
   }
}
